<?php
App::uses('AppController', 'Controller');
/**
 * Consignments Controller
 *
 * @property Consignment $Consignment
 */
class ConsignmentsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Consignment->recursive = 0;
		$this->set('consignments', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Consignment->exists($id)) {
			throw new NotFoundException(__('Invalid consignment'));
		}
		$options = array('conditions' => array('Consignment.' . $this->Consignment->primaryKey => $id));
		$this->set('consignment', $this->Consignment->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Consignment->create();
			if ($this->Consignment->save($this->request->data)) {
                $this->Consignment->saveField('date', date('Y-m-d H:i:s'));
                $this->Session->setFlash(__('The consignment has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The consignment could not be saved. Please, try again.'));
			}
		}
		$items = $this->Consignment->Item->find('list');
        $brands = $this->Consignment->Item->Brand->find('list');
        $colors = $this->Consignment->Item->Color->find('list');
        $conditions = $this->Consignment->Item->Condition->find('list');
        $consigners = $this->Consignment->Consigner->find('list');
        $sizes = $this->Consignment->Item->Size->find('list');
        $categories = $this->Consignment->Item->Category->find('list');
        /*$itemStatuses = $this ->consignement->Item->Itemstatus('list')*/
        $this->set(compact('items', 'consigners', 'brands', 'colors', 'conditions', 'sizes','categories'/*'itemStatuses'*/));




	}

/**
 * edit metho   d
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Consignment->exists($id)) {
			throw new NotFoundException(__('Invalid consignment'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Consignment->save($this->request->data)) {
				$this->Session->setFlash(__('The consignment has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The consignment could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Consignment.' . $this->Consignment->primaryKey => $id));
			$this->request->data = $this->Consignment->find('first', $options);
		}
		$items = $this->Consignment->Item->find('list');
		$consigners = $this->Consignment->Consigner->find('list');
		$this->set(compact('items', 'consigners'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Consignment->id = $id;
		if (!$this->Consignment->exists()) {
			throw new NotFoundException(__('Invalid consignment'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Consignment->delete()) {
			$this->Session->setFlash(__('Consignment deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Consignment was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}



